package com.zwb.service.controller;

import com.zwb.service.common.CacheConstant;
import com.zwb.service.pojo.Salary;
import com.zwb.service.service.ISalaryService;
import com.zwb.service.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 头发又黑又长
 * @Date 2022/9/11 15:32
 * @email zwb15083976291@163.com
 */
@Api(tags = "工资账套管理")
@Slf4j
@CacheConfig(cacheNames = CacheConstant.CACHEABLE_VALUE_3_DAT)
@RestController
@RequestMapping("/salary/sob")
public class SalaryController {

    @Autowired
    private ISalaryService salaryService;

    @ApiOperation("获取所有工资账套")
    @Cacheable(key = "'salaryAll'")
    @GetMapping("/")
    public R<List<Salary>> getSalaryAll() {
        return R.success(salaryService.list());
    }

    @ApiOperation("添加工资账套")
    @CacheEvict(key = "'salaryAll'")
    @PostMapping("/")
    public R addSalary(@RequestBody Salary salary) {
        salaryService.save(salary);
        return R.success("添加工资账套成功");
    }

    @ApiOperation("修改工资账套")
    @CacheEvict(key = "'salaryAll'")
    @PutMapping("/")
    public R updateSalary(@RequestBody Salary salary) {
        if (ObjectUtils.isEmpty(salary.getId()) || ObjectUtils.isEmpty(salaryService.getById(salary.getId()))) {
            return R.error("工资账套不存在");
        }
        salaryService.updateById(salary);
        return R.success("修改工资账套成功");
    }


    @ApiOperation("删除工资账套")
    @CacheEvict(key = "salaryAll")
    @DeleteMapping("/{id}")
    public R delSalary(@PathVariable("id") Integer id) {
        salaryService.removeById(id);
        return R.success("删除工资账套成功");

    }

}
